﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using CMISLibrary;
using System.Globalization;
using System.Data.OracleClient;
using BCNPCObject.DB;

namespace BCNPCObject.EXCEL
{
    public class cls_CN_PHIEU_THU_Controller
    {
        #region   Atributes

        private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private cls_CN_PHIEU_THU_Info info;
        private CN_PHIEU_THU infoNew;
        private List<CN_PHIEU_THU> lstInfo;
        #endregion

        #region   Constructor

        public cls_CN_PHIEU_THU_Controller()
        {
            info = new cls_CN_PHIEU_THU_Info();
            lstInfo = new List<CN_PHIEU_THU>();
            infoNew = new CN_PHIEU_THU();
        }
        public cls_CN_PHIEU_THU_Controller(cls_CN_PHIEU_THU_Info Info)
        {
            this.info = Info;
        }
        public cls_CN_PHIEU_THU_Controller(List<CN_PHIEU_THU> LstInfo, int i)
        {
            this.lstInfo = LstInfo;
        }
        public cls_CN_PHIEU_THU_Controller(CN_PHIEU_THU InfoNew, int i, int j)
        {
            this.infoNew = InfoNew;
        }
        #endregion

        #region   Properties

        public cls_CN_PHIEU_THU_Info pInfor
        {
            get { return info; }
            set { info = value; }
        }
        public List<CN_PHIEU_THU> pLstInfo
        {
            get { return lstInfo; }
            set { lstInfo = value; }
        }
        public CN_PHIEU_THU Infornew
        {
            get { return Infornew; }
            set { Infornew = value; }
        }
        
        #endregion

        #region Method tungnd
        public long getSoPThu()
        {
            try
            {
                long lngSeqLog = CMIS2.DB.ExecuteCommand("SELECT SEQ_CN_PHIEU_THU.NEXTVAL FROM DUAL");
                return lngSeqLog;
            }
            catch
            {
                return -1;
            }
        }
        public bool insert_PhieuThu( ref long _SO_PHTHU)
        {
            long _ID = getSoPThu();
            _SO_PHTHU = _ID;
            if (_ID == -1) return false;
            try
            {
                CN_PHIEU_THU obj = new CN_PHIEU_THU()
                {
                    SO_PHTHU = _ID,
                    MA_DVIQLY = info.MA_DVIQLY,
                    MA_KHANG = info.MA_KHANG,
                    MA_KHTT = info.MA_KHTT,
                    MA_PTTT = info.MA_PTTT.Trim(),
                    MA_TNGAN = info.MA_TNGAN,
                    SO_TIEN = info.SO_TIEN,
                    TIEN_THUA = info.TIEN_THUA,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGUOI_SUA = info.NGUOI_SUA,
                    NGAY_TAO = info.NGAY_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_NOP = info.NGAY_NOP,
                    SO_BBQTOAN = info.SO_BBQTOAN,
                    TSO_HDON = info.TSO_HDON
                };
                CMIS2.DB.CN_PHIEU_THU.InsertOnSubmit(obj);
                CMIS2.DB.SubmitChanges();
                //CMIS2.ResetDB();
                return true;
            }
            catch
            {
                CMIS2.ResetDB(); 
                return false;
            }
        }
        public bool delete_PhieuThu(string sma_DviQly, string sMa_KHANG, string s_Ma_TNGAN, decimal s_So_TIEN)
        {
            try
            {
                var query = from p in CMIS2.DB.CN_PHIEU_THU
                            where (p.MA_DVIQLY == sma_DviQly && p.MA_KHANG == sMa_KHANG && p.MA_TNGAN == s_Ma_TNGAN && p.SO_TIEN == s_So_TIEN)
                            select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.CN_PHIEU_THU.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return false;
            }
            catch
            {
                //CMIS2.ResetDB();
                return false;
            }
        }
        #endregion
    }
}
